<!--

    Copyright (C) 2015 The Gravitee team (http://gravitee.io)

    Licensed under the Apache License, Version 2.0 (the "License");
    you may not use this file except in compliance with the License.
    You may obtain a copy of the License at

            http://www.apache.org/licenses/LICENSE-2.0

    Unless required by applicable law or agreed to in writing, software
    distributed under the License is distributed on an "AS IS" BASIS,
    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    See the License for the specific language governing permissions and
    limitations under the License.

-->
<div class="gv-page-container">
  <a [routerLink]="['..']" class="gv-back-link"><small><< Back to roles</small></a>
  <h1>{{role.name}}</h1>
  <div fxLayout="row">
    <div fxLayout="column" fxFlex="70">
      <form (keydown.enter)="false" (ngSubmit)="update()" #roleForm="ngForm" fxLayout="column">
        <div class="gv-form-section">
          <mat-form-field appearance="outline" floatLabel="always">
            <mat-label>Name</mat-label>
            <input matInput type="text" placeholder="Name" name="name" [(ngModel)]="role.name" required [readonly]="!editMode">
            <mat-hint>A name for your role.</mat-hint>
          </mat-form-field>

          <mat-form-field appearance="outline" floatLabel="always">
            <mat-label>Description</mat-label>
            <input matInput type="text" placeholder="Description" name="description" [(ngModel)]="role.description" [readonly]="!editMode">
            <mat-hint>A description for your role.</mat-hint>
          </mat-form-field>
        </div>

        <div class="gv-form-section">
          <div class="gv-form-section-title">
            <h5>Permissions</h5>
            <mat-divider></mat-divider>
          </div>
          <mat-form-field appearance="outline" floatLabel="always">
            <mat-chip-list style="margin-top: 10px; margin-bottom: 10px;" #chipListPermissions>
              <mat-chip *ngFor="let permission of selectedPermissions" color="primary"
                        selected="true" (removed)="removePermission(permission)">
                <mat-icon style="vertical-align:middle;">donut_large</mat-icon>
                &nbsp;<span>{{permission.key}}</span>&nbsp;|&nbsp;<small>{{permission.name}}</small>
                <mat-icon matChipRemove>cancel</mat-icon>
              </mat-chip>
              <input matInput
                     placeholder="Permissions"
                     aria-label="Permissions"
                     [matAutocomplete]="auto"
                     [matChipInputFor]="chipListPermissions"
                     [readonly]="!editMode"
                     #chipInput>
            </mat-chip-list>

            <mat-autocomplete #auto="matAutocomplete" (optionSelected)="addPermission($event)">
              <mat-option *ngFor="let scope of scopes" [value]="scope.key">
                <mat-icon style="vertical-align:middle;">donut_large</mat-icon>
                <span>{{ scope.key }}</span> | <small>{{scope.name}}</small>
              </mat-option>
            </mat-autocomplete>
            <mat-hint>Role's permissions</mat-hint>
          </mat-form-field>
        </div>

        <div fxLayout="row" *hasPermission="['domain_role_update']">
          <button mat-raised-button [disabled]="(!roleForm.valid || roleForm.pristine) && !formChanged" type="submit">SAVE</button>
        </div>
      </form>
      <div class="gv-page-delete-zone" fxLayout="column" *hasPermission="['domain_role_delete']">
        <p>This operation cannot be undone.</p>
        Role will be deleted, users and groups will not be able to use this role's permissions.
        <div class="gv-page-delete-button" fxLayout="row">
          <span fxFlex></span>
          <button mat-raised-button color="warn" (click)="delete($event)">DELETE</button>
        </div>
      </div>
    </div>
    <div class="gv-page-description" fxFlex>
      <h3>Manage role</h3>
      <div class="gv-page-description-content">
        <p>
          Configure your role's name, description and permissions. Permissions can be used to configure access scopes to grant specific access to a your APIs.
        </p>
        <p>
          For example you can create a <code>member</code> role with <code>read</code> permission to grant readonly access to your protected resources.
        </p>
      </div>
    </div>
  </div>
</div>

